home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / X11 / wais / waisgate / HTParse.c < prev    next >
C/C++ Source or Header  |  1995-05-09  |  12KB  |  437 lines

  1. /*        Parse HyperText Document Address        HTParse.c
  2. **        ================================
  3. */
  4.  
  5. #include "HTUtils.h"
  6. #include "HTParse.h"
  7. #include "tcp.h"
  8.  
  9. #define HEX_ESCAPE '%'
  10.  
  11. struct struct_parts {
  12.     char * access;
  13.     char * host;
  14.     char * absolute;
  15.     char * relative;
  16. /*    char * search;        no - treated as part of path */
  17.     char * anchor;
  18. };
  19.  
  20.  
  21. /*    Strip white space off a string
  22. **    ------------------------------
  23. **
  24. ** On exit,
  25. **    Return value points to first non-white character, or to 0 if none.
  26. **    All trailing white space is OVERWRITTEN with zero.
  27. */
  28.  
  29. #ifdef __STDC__
  30. char * HTStrip(char * s)
  31. #else
  32. char * HTStrip(s)
  33.     char *s;
  34. #endif
  35. {
  36. #define SPACE(c) ((c==' ')||(c=='\t')||(c=='\n')) 
  37.     char * p=s;
  38.     for(p=s;*p;p++);                /* Find end of string */
  39.     for(p--;p>=s;p--) {
  40.         if(SPACE(*p)) *p=0;    /* Zap trailing blanks */
  41.     else break;
  42.     }
  43.     while(SPACE(*s))s++;    /* Strip leading blanks */
  44.     return s;
  45. }
  46.  
  47.  
  48. /*    Scan a filename for its consituents
  49. **    -----------------------------------
  50. **
  51. ** On entry,
  52. **    name    points to a document name which may be incomplete.
  53. ** On exit,
  54. **      absolute or relative may be nonzero (but not both).
  55. **    host, anchor and access may be nonzero if they were specified.
  56. **    Any which are nonzero point to zero terminated strings.
  57. */
  58. #ifdef __STDC__
  59. PRIVATE void scan(char * name, struct struct_parts *parts)
  60. #else
  61. PRIVATE void scan(name, parts)
  62.     char * name;
  63.     struct struct_parts *parts;
  64. #endif
  65. {
  66.     char * after_access;
  67.     char * p;
  68.     int length = strlen(name);
  69.     
  70.     parts->access = 0;
  71.     parts->host = 0;
  72.     parts->absolute = 0;
  73.     parts->relative = 0;
  74.     parts->anchor = 0;
  75.     
  76.     after_access = name;
  77.     for(p=name; *p; p++) {
  78.     if (*p==':') {
  79.         *p = 0;
  80.         parts->access = name;    /* Access name has been specified */
  81.         after_access = p+1;
  82.     }
  83.     if (*p=='/') break;
  84.     if (*p=='#') break;
  85.     }
  86.     
  87.     for(p=name+length-1; p>=name; p--) {
  88.     if (*p =='#') {
  89.         parts->anchor=p+1;
  90.         *p=0;                /* terminate the rest */
  91.     }
  92.     }
  93.     p = after_access;
  94.     if (*p=='/'){
  95.     if (p[1]=='/') {
  96.         parts->host = p+2;        /* host has been specified     */
  97.         *p=0;            /* Terminate access         */
  98.         p=strchr(parts->host,'/');    /* look for end of host name if any */
  99.         if(p) {
  100.             *p=0;            /* Terminate host */
  101.             parts->absolute = p+1;        /* Root has been found */
  102.         }
  103.     } else {
  104.         parts->absolute = p+1;        /* Root found but no host */
  105.     }        
  106.     } else {
  107.         parts->relative = (*after_access) ? after_access : 0;    /* zero for "" */
  108.     }
  109.  
  110.     /* Access specified but no host: the anchor was not really one
  111.        e.g. news:j462#36487@foo.bar -- JFG 10/7/92, from bug report */
  112.     if (parts->access && ! parts->host && parts->anchor) {
  113.       *(parts->anchor - 1) = '#';  /* Restore the '#' in the address */
  114.       parts->anchor = 0;
  115.     }
  116.  
  117. #ifdef NOT_DEFINED    /* search is just treated as part of path */
  118.     {
  119.         char *p = relative ? relative : absolute;
  120.     if (p) {
  121.         char * q = strchr(p, '?');    /* Any search string? */
  122.         if (q) {
  123.             *q = 0;            /* If so, chop that off. */
  124.         parts->search = q+1;
  125.         }
  126.     }
  127.     }
  128. #endif
  129. } /*scan */    
  130.  
  131.  
  132. /*    Parse a Name relative to another name
  133. **    -------------------------------------
  134. **
  135. **    This returns those parts of a name which are given (and requested)
  136. **    substituting bits from the related name where necessary.
  137. **
  138. ** On entry,
  139. **    aName        A filename given
  140. **      relatedName     A name relative to which aName is to be parsed
  141. **      wanted          A mask for the bits which are wanted.
  142. **
  143. ** On exit,
  144. **    returns        A pointer to a malloc'd string which MUST BE FREED
  145. */
  146. #ifdef __STDC__
  147. char * HTParse(const char * aName, const char * relatedName, int wanted)
  148. #else
  149. char * HTParse(aName, relatedName, wanted)
  150.     char * aName;
  151.     char * relatedName;
  152.     int wanted;
  153. #endif
  154.  
  155. {
  156.     char * result = 0;
  157.     char * return_value = 0;
  158.     int len;
  159.     char * name = 0;
  160.     char * rel = 0;
  161.     char * p;
  162.     struct struct_parts given, related;
  163.     
  164.     /* Make working copies of input strings to cut up:
  165.     */
  166.     len = strlen(aName)+strlen(relatedName)+10;
  167.     result=(char *)malloc(len);        /* Lots of space: more than enough */
  168.     if (result == NULL) outofmem(__FILE__, "HTParse");
  169.     
  170.     StrAllocCopy(name, aName);
  171.     StrAllocCopy(rel, relatedName);
  172.     
  173.     scan(name, &given);
  174.     scan(rel,  &related); 
  175.     result[0]=0;        /* Clear string  */
  176.     if (wanted & PARSE_ACCESS)
  177.         if (given.access|| related.access) {
  178.         strcat(result, given.access ? given.access : related.access);
  179.         if(wanted & PARSE_PUNCTUATION) strcat(result, ":");
  180.     }
  181.     
  182.     if (given.access && related.access)    /* If different, inherit nothing. */
  183.         if (strcmp(given.access, related.access)!=0) {
  184.         related.host=0;
  185.         related.absolute=0;
  186.         related.relative=0;
  187.         related.anchor=0;
  188.     }
  189.     
  190.     if (wanted & PARSE_HOST)
  191.         if(given.host || related.host) {
  192.         if(wanted & PARSE_PUNCTUATION) strcat(result, "//");
  193.         strcat(result, given.host ? given.host : related.host);
  194.     }
  195.     
  196.     if (given.host && related.host)  /* If different hosts, inherit no path. */
  197.         if (strcmp(given.host, related.host)!=0) {
  198.         related.absolute=0;
  199.         related.relative=0;
  200.         related.anchor=0;
  201.     }
  202.     
  203.     if (wanted & PARSE_PATH) {
  204.         if(given.absolute) {                /* All is given */
  205.         if(wanted & PARSE_PUNCTUATION) strcat(result, "/");
  206.         strcat(result, given.absolute);
  207.     } else if(related.absolute) {    /* Adopt path not name */
  208.         strcat(result, "/");
  209.         strcat(result, related.absolute);
  210.         if (given.relative) {
  211.         p = strchr(result, '?');    /* Search part? */
  212.         if (!p) p=result+strlen(result)-1;
  213.         for (; *p!='/'; p--);    /* last / */
  214.         p[1]=0;                    /* Remove filename */
  215.         strcat(result, given.relative);        /* Add given one */
  216.         HTSimplify (result);
  217.         }
  218.     } else if(given.relative) {
  219.         strcat(result, given.relative);        /* what we've got */
  220.     } else if(related.relative) {
  221.         strcat(result, related.relative);
  222.     } else {  /* No inheritance */
  223.         strcat(result, "/");
  224.     }
  225.     }
  226.         
  227.     if (wanted & PARSE_ANCHOR)
  228.         if(given.anchor || related.anchor) {
  229.         if(wanted & PARSE_PUNCTUATION) strcat(result, "#");
  230.         strcat(result, given.anchor ? given.anchor : related.anchor);
  231.     }
  232.     free(rel);
  233.     free(name);
  234.     
  235.     StrAllocCopy(return_value, result);
  236.     free(result);
  237.     return return_value;        /* exactly the right length */
  238. }
  239.  
  240. /*            Simplify a filename
  241. //        -------------------
  242. //
  243. // A unix-style file is allowed to contain the seqeunce xxx/../ which may be
  244. // replaced by "" , and the seqeunce "/./" which may be replaced by "/".
  245. // Simplification helps us recognize duplicate filenames.
  246. //
  247. //    Thus,     /etc/junk/../fred     becomes    /etc/fred
  248. //        /etc/junk/./fred    becomes    /etc/junk/fred
  249. */
  250. #ifdef __STDC__
  251. void HTSimplify(char * filename)
  252. #else
  253. void HTSimplify(filename)
  254.     char * filename;
  255. #endif
  256.  
  257. {
  258.     char * p;
  259.     char * q;
  260.     for(p=filename+2; *p; p++) {
  261.         if (*p=='/') {
  262.         if ((p[1]=='.') && (p[2]=='.') && (p[3]=='/' || !p[3] )) {
  263.         for (q=p-1; (q>filename) && (*q!='/'); q--); /* prev slash */
  264.         if (*q=='/') {
  265.                 strcpy(q, p+3);    /* Remove  /xxx/..    */
  266.             if (!*filename) strcpy(filename, "/");
  267.             p = q-1;        /* Start again with prev slash     */
  268.         } else {            /*   xxx/..    error?    */
  269.             strcpy(filename, p[3] ? p+4 : p+3); /* rm  xxx/../    */
  270.             p = filename;        /* Start again */
  271.         }
  272.         } else if ((p[1]=='.') && (p[2]=='/' || !p[2])) {
  273.             strcpy(p, p+2);            /* Remove a slash and a dot */
  274.         }
  275.     }
  276.     }
  277. }
  278.  
  279.  
  280. /*        Make Relative Name
  281. **        ------------------
  282. **
  283. ** This Function creates and returns a string which gives an expression of
  284. ** one address as related to another. Where there is no relation, an absolute
  285. ** address is retured.
  286. **
  287. **  On entry,
  288. **    Both names must be absolute, fully qualified names of nodes
  289. **    (no anchor bits)
  290. **
  291. **  On exit,
  292. **    The return result points to a newly allocated name which, if
  293. **    parsed by HTParse relative to relatedName, will yield aName.
  294. **    The caller is responsible for freeing the resulting name later.
  295. **
  296. */
  297. #ifdef __STDC__
  298. char * HTRelative(const char * aName, const char *relatedName)
  299. #else
  300. char * HTRelative(aName, relatedName)
  301.    char * aName;
  302.    char * relatedName;
  303. #endif
  304. {
  305.     char * result = 0;
  306.     const char *p = aName;
  307.     const char *q = relatedName;
  308.     const char * after_access = 0;
  309.     const char * path = 0;
  310.     const char * last_slash = 0;
  311.     int slashes = 0;
  312.     
  313.     for(;*p; p++, q++) {    /* Find extent of match */
  314.         if (*p!=*q) break;
  315.     if (*p==':') after_access = p+1;
  316.     if (*p=='/') {
  317.         last_slash = p;
  318.         slashes++;
  319.         if (slashes==3) path=p;
  320.     }
  321.     }
  322.     
  323.     /* q, p point to the first non-matching character or zero */
  324.     
  325.     if (!after_access) {            /* Different access */
  326.         StrAllocCopy(result, aName);
  327.     } else if (slashes<3){            /* Different nodes */
  328.         StrAllocCopy(result, after_access);
  329.     } else if (slashes==3){            /* Same node, different path */
  330.         StrAllocCopy(result, path);
  331.     } else {                    /* Some path in common */
  332.         int levels= 0;
  333.         for(; *q && (*q!='#'); q++)  if (*q=='/') levels++;
  334.     result = (char *)malloc(3*levels + strlen(last_slash) + 1);
  335.       if (result == NULL) outofmem(__FILE__, "HTRelative");
  336.     result[0]=0;
  337.     for(;levels; levels--)strcat(result, "../");
  338.     strcat(result, last_slash+1);
  339.     }
  340.     if (TRACE) fprintf(stderr, "HT: `%s' expressed relative to\n    `%s' is\n   `%s'.",
  341.             aName, relatedName, result);
  342.     return result;
  343. }
  344.  
  345.  
  346. /*        Escape undesirable characters using %        HTEscape()
  347. **        -------------------------------------
  348. **
  349. **    This Function takes a pointer to a string in which
  350. **    some characters may be unacceptable unescaped.
  351. **    It returns a string which has these characters
  352. **    represented by a '%' character followed by two hex digits.
  353. **
  354. **    Unlike HTUnEscape(), this routine returns a malloced string.
  355. */
  356.  
  357. PRIVATE CONST unsigned char isAcceptable[96] =
  358.  
  359. /*    Bit 0        xalpha        -- see HTFile.h
  360. **    Bit 1        xpalpha        -- as xalpha but with plus.
  361. **    Bit 3 ...    path        -- as xpalphas but with /
  362. */
  363.     /*   0 1 2 3 4 5 6 7 8 9 A B C D E F */
  364.     {    0,0,0,0,0,0,0,0,0,0,7,6,0,7,7,4,    /* 2x   !"#$%&'()*+,-./     */
  365.          7,7,7,7,7,7,7,7,7,7,0,0,0,0,0,0,    /* 3x  0123456789:;<=>?     */
  366.      7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,    /* 4x  @ABCDEFGHIJKLMNO  */
  367.      7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,7,    /* 5X  PQRSTUVWXYZ[\]^_     */
  368.      0,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,    /* 6x  `abcdefghijklmno     */
  369.      7,7,7,7,7,7,7,7,7,7,7,0,0,0,0,0 };    /* 7X  pqrstuvwxyz{\}~    DEL */
  370.  
  371. PRIVATE char *hex = "0123456789ABCDEF";
  372.  
  373. PUBLIC char * HTEscape ARGS2 (char *, str,
  374.     unsigned char, mask)
  375. {
  376. #define ACCEPTABLE(a)    ( a>=32 && a<128 && ((isAcceptable[a-32]) & mask))
  377.     CONST char * p;
  378.     char * q;
  379.     char * result;
  380.     int unacceptable = 0;
  381.     for(p=str; *p; p++)
  382.         if (!ACCEPTABLE((unsigned char)TOASCII(*p)))
  383.         unacceptable++;
  384.     result = (char *) malloc(p-str + unacceptable+ unacceptable + 1);
  385.     if (result == NULL) outofmem(__FILE__, "HTEscape");
  386.     for(q=result, p=str; *p; p++) {
  387.         unsigned char a = TOASCII(*p);
  388.     if (!ACCEPTABLE(a)) {
  389.         *q++ = HEX_ESCAPE;    /* Means hex commming */
  390.         *q++ = hex[a >> 4];
  391.         *q++ = hex[a & 15];
  392.     }
  393.     else *q++ = *p;
  394.     }
  395.     *q++ = 0;            /* Terminate */
  396.     return result;
  397. }
  398.  
  399.  
  400. /*        Decode %xx escaped characters            HTUnEscape()
  401. **        -----------------------------
  402. **
  403. **    This Function takes a pointer to a string in which some
  404. **    characters may have been encoded in %xy form, where xy is
  405. **    the acsii hex code for character 16x+y.
  406. **    The string is converted in place, as it will never grow.
  407. */
  408.  
  409. PRIVATE char from_hex ARGS1(char, c)
  410. {
  411.     return  c >= '0' && c <= '9' ?  c - '0' 
  412.             : c >= 'A' && c <= 'F'? c - 'A' + 10
  413.             : c - 'a' + 10;    /* accept small letters just in case */
  414. }
  415.  
  416. PUBLIC char * HTUnEscape ARGS1( char *, str)
  417. {
  418.     char * p = str;
  419.     char * q = str;
  420.     while(*p) {
  421.         if (*p == HEX_ESCAPE) {
  422.         p++;
  423.         if (*p) *q = from_hex(*p++) * 16;
  424.         if (*p) *q = FROMASCII(*q + from_hex(*p++));
  425.         q++;
  426.     } else {
  427.         *q++ = *p++; 
  428.     }
  429.     }
  430.     
  431.     *q++ = 0;
  432.     return str;
  433.     
  434. } /* HTUnEscape */
  435.  
  436.  
  437.